<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ include file="../../../attr.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=emulateIE7"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="${ctx}/css/sapar.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/common.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/kkpager_blue.css"/>



    <style type="text/css">
        td {
            text-align: center;
        }
    </style>
</head>
<body>
<div class="tab-content-item">
    <form method="post" id="dataWuTiaomaHuiShouForm" name="dataWuTiaomaHuiShouForm">
        <div class="kv-item clearfix">


        </div>
        <div class="kv-item clearfix">
            <label>包筛选(简)：</label>
            <div class="kv-item-content" style="line-height:30px;">
                <input name="packageName" id="packageName" class="gui-input" placeholder="包简码" type="text" value="">
            </div>
            <label></label><a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn" id="baocun"
                              onclick="do_save()">保存</a>
        </div>

        <div class="table">
            <!--表格具体内容-->
            <div class="table-box">
                <table id="tbpck">
                    <thead>
                    <tr>
                        <th>部门</th>
                        <th>包名</th>
                        <th>数量</th>
                    </tr>
                    </thead>
                    <tbody id="body_list">


                    </tbody>
                </table>
            </div>
            <div>
                <input id="leftPage" class="butLeft" type="button" value="上一页"/>
                <input id="rightPage" class="butRight" type="button" value="下一页"/>
                <span>到第&nbsp;</span><input id="goPage" class="textBox" type="text" value="1"/><span
                    style="margin-left: 4px;">页&nbsp;</span>
                <input id="sureBut" class="sure" type="button" value="确定"/>
                <span>共</span><span class="totalPages" id="totalNum">0</span><span>&nbsp;页</span>
            </div>
        </div>
    </form>
</div>

</body>
<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/kkpager.min.js"></script>
<%@ include file="../../../js.jsp" %>
<script type="text/javascript">

    window.onload = function () {
        $("#packageName").focus();
    };

    var pageSize = 9, currentPage = 1, totalPage;
    var data = [];
    data = ${packages};
    appendTable(data);
    $(function () {

        $("#sureBut").click(function () {
            goPage();
        });
        $("#leftPage").click(function () {
            leftPages();
        });
        $("#rightPage").click(function () {
            rightPages();
        });

        $("#back").click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });
        $("#save").click(function () {
            do_save();
        });

        $(document).keydown(function (event) {
            var oPlay0 = document.getElementById("audio");
            var oPlay1 = document.getElementById("audio1");
            var packageName = $("input[name='packageName']").val();
            if (event.keyCode == 13) {
                if ($("#packageName").is(":focus") && !isNullOrEmpty(packageName)) {
                    showPack();
                }
            }
        });


    });

    function showPack() {
        var url = "${ctx}/wutiaoma/findHuishouPackageByDepart.do?packageCode=" + $("#packageName").val() + "&packageDepartid=" + $("#packageDepartid").val();
        $.ajax({
            type: "POST",
            url: url,
            data: $('#dataHuiShouForm').serialize(),// 你的formid
            error: function (request) {
                layer.alert("网络故障，请稍后再试!");
            },
            success: function (data) {
                if (data.status == "true") {
                    appendTable(data.packages);
                }
            }
        });
    }


    function do_save() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        var rtnJson = new Array();
        var check = true;
        var options = $("#packageDepartid option:selected");
        if(options==null){
            alert("请选择科室");
        }
        var keshi = $("#packageDepartid").val()
        if (keshi != "0") {
            var keshiName = $("#packageDepartid").find("option:selected").text();
        }else if(keshi == "0"){
            check = false;
        }
        $('input[name="key"]').each(function () {
            var depVal = $(this).prev().prev().val();
            var id = $(this).prev().val();
            var name = $(this).next().val();
            var val = $(this).val();
            var depId = $("#packageDepartid").val();
            if (val != '') {
                var obj = new Object();
                obj.keshi_packId = keshi+id;
                obj.keshi = keshi;
                obj.keshiName = keshiName;
                obj.packId = id;
                obj.packName = name;
                obj.packNum = val;
                obj.depId = depId;
                obj.depVal = depVal;
                rtnJson.push(obj);
                //console.log(rtnJson);
            }
        });
        if(check){
            parent.callbackData(rtnJson);
            parent.layer.close(index);
        }else{
            alert("请选择科室进行回收，方便统计");
        }

    }

    function appendTable(datalist) {
        currentPage = 1;
        data = datalist;
//       packageId = dataList.packageId;
        creatTab();
    }

    //获得总页数
    function totalPages() {
        //如果刚好除尽
        if (data.length % pageSize == 0) {
            totalPage = data.length / pageSize;
        }
        else {
            totalPage = parseInt(data.length / pageSize) + 1;
        }
    }

    //上一页
    function leftPages() {
        if (currentPage != 1) {
            currentPage--;
            //开始创建
            creatTab();
        }
    }
    //下一页
    function rightPages() {
        if (currentPage != totalPage) {
            currentPage++;
            //开始创建
            creatTab();
        }
    }
    //到第几页去
    function goPage() {
        currentPage = document.getElementById('goPage').value;
        //如果输入的页数超出总页数，则到最后一页去！
        if (currentPage > totalPage) {
            currentPage = totalPage;
        }
        if (currentPage < 1) {
            currentPage = 1;
        }
        creatTab();
    }

    function creatTab() {
        //清空数据
        $("#tbpck tbody").empty();
//  	$("#checkalltwo").attr("checked",false);
        //总页数
        totalPages(data);
        $('#totalNum').text(totalPage);
        //插入页数
        $('#goPage').val(currentPage);
        var str = '';
//		$("#tbpck tbody").empty();
        currentRows = (totalPage == 1 || currentPage == totalPage) ? data.length : currentPage * pageSize;
        console.log(currentPage);
        console.log(currentRows);

        for (var i = (currentPage - 1) * pageSize; i < currentRows; i++) {
            var dep = data[i].tbDepartment;
            var deptname;
            if (dep != null) {
                deptname = dep.departmentName;
            } else {
                deptname = "";
            }

            str += '<tr><td>' + deptname + '</td><td class="text-left">' + data[i].packageName + '</td><td><input type="hidden" name="deptName" value="' + deptname + '"/><input type="hidden" name="pid" value="' + data[i].packageId + '"/><input style="text-align:center" type="text" name="key" maxlength="3" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" class="gui-input"/><input type="hidden" name="pname" value="' + data[i].packageName + '"/></td></tr>'
        }
        $("#tbpck tbody").append(str);
    }

    //判断是否为空
    function isNullOrEmpty(strVal) {
        if (strVal != null) {
            strVal = strVal.replace(/(^\s*)|(\s*$)/g, "");
        }
        if (strVal == '' || strVal == null || strVal == undefined) {
            return true;
        } else {
            return false;
        }
    }

</script>
</html>